import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

class Cust {  
    List<Bill> bills = new ArrayList<>(); 
    double monthlyBudget;
    void setMonthlyBudget(double budget) {
        this.monthlyBudget = budget;
    }
    double getRemainingBudget() {
        double totalExpenses = 0;
        for (Bill bill : bills) {
            if (bill.money < 0) {
                totalExpenses += Math.abs(bill.money);
            }
        }
        return monthlyBudget - totalExpenses;
    }
    void Income() {  
        System.out.print("请输入日期(如2024.9.10)：");  
        String date = KB.scan();  
        System.out.print("请输入收入金额（必须为正数）：");  
        double amount = Double.parseDouble(KB.scan());  
        if (amount <= 0) {  
            System.out.println("收入金额必须为正数！");  
            return;  
        }  
        System.out.print("请输入类别：");  
        String type = KB.scan();  
        System.out.print("请输入备注：");  
        String note = KB.scan();  
        bills.add(new Bill(date, amount, type, note));  
    }  
  
    void Expense() {  
        System.out.print("请输入日期(如2024.9.10)：");  
        String date = KB.scan();  
        System.out.print("请输入支出金额（必须为正数）：");  
        double amount = Double.parseDouble(KB.scan());  
        if (amount <= 0) {  
            System.out.println("支出金额必须为正数！");  
            return;  
        }  
        System.out.print("请输入类别（如餐饮、交通、购物等）：");  
        String type = KB.scan();  
        System.out.print("请输入备注：");  
        String note = KB.scan();  
  
        bills.add(new Bill(date, -amount, type, note));  
    }  
  
    void showBills() {  
        System.out.println("收入账单：");  
        for (Bill bill : bills) {  
            if (bill.money > 0) {  
                System.out.println(bill.date + " " + bill.money + " " + bill.type + " " + bill.note);  
            }  
        }  
  
        System.out.println("支出账单：");  
        for (Bill bill : bills) {  
            if (bill.money < 0) {  
                System.out.println(bill.date + " " + (-bill.money) + " " + bill.type + " " + bill.note);  
            }  
        }  
    }  
  
    void queryBillsByDate(String date) throws ParseException {  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.M.dd");  
        Date queryDate = sdf.parse(date);  
  
        System.out.println("收入账单：");  
        for (Bill bill : bills) {  
            Date billDate = sdf.parse(bill.date);  
            if (bill.money > 0 && billDate.equals(queryDate)) {  
                System.out.println(bill.date + " " + bill.money + " " + bill.type + " " + bill.note);  
            }  
        }  
  
        System.out.println("支出账单：");  
        for (Bill bill : bills) {  
            Date billDate = sdf.parse(bill.date);  
            if (bill.money < 0 && billDate.equals(queryDate)) {  
                System.out.println(bill.date + " " + (-bill.money) + " " + bill.type + " " + bill.note);  
            }  
        }  
    }  
  
    void queryBillsByDateRange(String startDate, String endDate) throws ParseException {  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.M.dd");  
        Date start = sdf.parse(startDate);  
        Date end = sdf.parse(endDate);  
  
        System.out.println("收入账单：");  
        for (Bill bill : bills) {  
            Date billDate = sdf.parse(bill.date);  
            if (bill.money > 0 && billDate.after(start) && billDate.before(end)) {  
                System.out.println(bill.date + " " + bill.money + " " + bill.type + " " + bill.note);  
            }  
        }  
  
        System.out.println("支出账单：");  
        for (Bill bill : bills) {  
            Date billDate = sdf.parse(bill.date);  
            if (bill.money < 0 && billDate.after(start) && billDate.before(end)) {  
                System.out.println(bill.date + " " + (-bill.money) + " " + bill.type + " " + bill.note);  
            }  
        }  
    }  
  
    void  queryBillsByType(String type) {  
        System.out.println("收入账单：");  
        for (Bill bill : bills) {  
            if (bill.money > 0 && bill.type.equalsIgnoreCase(type)) {  
                System.out.println(bill.date + " " + bill.money + " " + bill.type + " " + bill.note);  
            }  
        }  
  
        System.out.println("支出账单：");  
        for (Bill bill : bills) {  
            if (bill.money < 0 && bill.type.equalsIgnoreCase(type)) {  
                System.out.println(bill.date + " " + (-bill.money) + " " + bill.type + " " + bill.note);  
            }  
        }  
    }

    void monthlyStatistics() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.M");
        Map<String, Double> typeMap = new HashMap<>(); // 存储各类别的金额

        for (Bill bill : bills) {
            try {
                Date billDate = sdf.parse(bill.date);
                String yearMonth = sdf.format(billDate); // 获取年月

                if (bill.money > 0) {
                    // 收入
                    System.out.println("收入：" + bill.money);
                } else {
                    // 支出
                    System.out.println("支出：" + (-bill.money));
                }

                // 统计各类别金额
                if (typeMap.containsKey(yearMonth)) {
                    typeMap.put(yearMonth, typeMap.get(yearMonth) + bill.money);
                } else {
                    typeMap.put(yearMonth, bill.money);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}  